﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	[TestClass]
	public class GenParse_Helper_Test
	{
		#region tests

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test01_GenerateNumberBytes()
		{
			{
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("0");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(1, idNumberBytes.Length);
				Assert.AreEqual(0xf0, idNumberBytes[0]);
			}

			{
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("1");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(1, idNumberBytes.Length);
				Assert.AreEqual(0xf1, idNumberBytes[0]);
			}

			{
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("8");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(1, idNumberBytes.Length);
				Assert.AreEqual(0xf8, idNumberBytes[0]);
			}

			{
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("08");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(1, idNumberBytes.Length);
				Assert.AreEqual(0x80, idNumberBytes[0]);
			}

			{
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("48");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(1, idNumberBytes.Length);
				Assert.AreEqual(0x84, idNumberBytes[0]);
			}

			{
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("99");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(1, idNumberBytes.Length);
				Assert.AreEqual(0x99, idNumberBytes[0]);
			}

			{
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("099");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(2, idNumberBytes.Length);
				Assert.AreEqual(0x90, idNumberBytes[0]);
				Assert.AreEqual(0xf9, idNumberBytes[1]);
			}

			{
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("123456789012");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(6, idNumberBytes.Length);
				Assert.AreEqual(0x21, idNumberBytes[0]);
				Assert.AreEqual(0x43, idNumberBytes[1]);
				Assert.AreEqual(0x65, idNumberBytes[2]);
				Assert.AreEqual(0x87, idNumberBytes[3]);
				Assert.AreEqual(0x09, idNumberBytes[4]);
				Assert.AreEqual(0x21, idNumberBytes[5]);
			}

			{
				// 12 digits - max value
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("999999999999");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(6, idNumberBytes.Length);
				Assert.AreEqual(0x99, idNumberBytes[0]);
				Assert.AreEqual(0x99, idNumberBytes[1]);
				Assert.AreEqual(0x99, idNumberBytes[2]);
				Assert.AreEqual(0x99, idNumberBytes[3]);
				Assert.AreEqual(0x99, idNumberBytes[4]);
				Assert.AreEqual(0x99, idNumberBytes[5]);
			}

			{
				// 11 digits
				byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes("12345678901");
				Assert.IsNotNull(idNumberBytes);
				Assert.AreEqual(6, idNumberBytes.Length);
				Assert.AreEqual(0x21, idNumberBytes[0]);
				Assert.AreEqual(0x43, idNumberBytes[1]);
				Assert.AreEqual(0x65, idNumberBytes[2]);
				Assert.AreEqual(0x87, idNumberBytes[3]);
				Assert.AreEqual(0x09, idNumberBytes[4]);
				Assert.AreEqual(0xF1, idNumberBytes[5]);
			}
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test02_ParseNumberBytes()
		{
			{
				byte[] idNumberBytes = new byte[] { 0xf0 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "0", false) == 0);
			}

			{
				byte[] idNumberBytes = new byte[] { 0xf1 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "1", false) == 0);
			}

			{
				byte[] idNumberBytes = new byte[] { 0xf8 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "8", false) == 0);
			}

			{
				byte[] idNumberBytes = new byte[] { 0x80 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "08", false) == 0);
			}

			{
				byte[] idNumberBytes = new byte[] { 0x84 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "48", false) == 0);
			}

			{
				byte[] idNumberBytes = new byte[] { 0x99 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "99", false) == 0);
			}

			{
				byte[] idNumberBytes = new byte[] { 0x90, 0xf9 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "099", false) == 0);
			}

			{
				byte[] idNumberBytes = new byte[] { 0x21, 0x43, 0x65, 0x87, 0x09, 0x21 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "123456789012", false) == 0);
			}

			{
				// 12 digits - max value
				byte[] idNumberBytes = new byte[] { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99 };
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "999999999999", false) == 0);
			}

			{
				// 11 digits
				byte[] idNumberBytes = new byte[] { 0x21, 0x43, 0x65, 0x87, 0x09, 0xF1};
				string numberString = GenParse_Helper.ParseNumberBytes(idNumberBytes);
				Assert.IsFalse(String.IsNullOrEmpty(numberString));
				Assert.IsTrue(String.Compare(numberString, "12345678901", false) == 0);
			}
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test03_GenerateNumberBytesTHENParseNumberBytes()
		{
			{
				const string numberString = "0";
				GenerateThenParse(numberString);
			}

			{
				const string numberString = "1";
				GenerateThenParse(numberString);
			}

			{
				const string numberString = "8";
				GenerateThenParse(numberString);
			}

			{
				const string numberString = "08";
				GenerateThenParse(numberString);
			}

			{
				const string numberString = "48";
				GenerateThenParse(numberString);
			}

			{
				const string numberString = "99";
				GenerateThenParse(numberString);
			}

			{
				const string numberString = "099";
				GenerateThenParse(numberString);
			}

			{
				const string numberString = "123456789012";
				GenerateThenParse(numberString);
			}

			{
				// 12 digits - max value
				const string numberString = "999999999999";
				GenerateThenParse(numberString);
			}

			{
				// 11 digits
				const string numberString = "12345678901";
				GenerateThenParse(numberString);
			}
		}

		#endregion tests

		#region private methods

		private static void GenerateThenParse(string numberString)
		{
			byte[] idNumberBytes = GenParse_Helper.GenerateNumberBytes(numberString);
			string numberString2Compare = GenParse_Helper.ParseNumberBytes(idNumberBytes);
			Assert.IsTrue(String.Compare(numberString, numberString2Compare, false) == 0);
		}

		#endregion private methods
	}
}
